package chapte

/**
 * 匹配常量
 */
object Test02_MatchTypes {
  def main(args: Array[String]): Unit = {
    def describeConst(x: Any): String = x match {
      case 1 => "Int One"
      case "hello" => "String hello"
      case true => "Boolean True"
      case '+' => "Char +"
      case abc => ""
    }

    println(describeConst("hello"))
    println(describeConst('+'))
    println(describeConst(0.3))

    /**
     * 1、测试代码
     * 匹配类型
     * 常量类型 匹配
     *
     * @param x
     * @return
     */
    def describeType(x: Any): String = x match {
      case i: Int => "Int " + i
      case s: String => "String " + s
      case list: List[String] => "List " + list
      case array: Array[Int] => "Array[Int] " + array.mkString(",")
      case a => "Something else: " + a
    }

    println(describeType(35))
    println(describeType("hello"))
    println(describeType(List("hi", "hello")))
    println(describeType(List(2, 23)))
    println(describeType(Array("hi", "hello")))
    println(describeType(Array(2, 23)))


    def getName(x: Any): String = x match {
      case 1 => "张"
      case 2 => "黄"

    }

    println(getName(2))


    /**
     * 1、测试代码
     * 匹配数组
     * 常量类型 匹配
     * 0
     * (1, 0)
     * 以零开头的数组
     * 中间为1的三元数组
     * something else
     * something else
     *
     * @return
     */
    for (arr <- List(
      Array(0),
      Array(1, 0),
      Array(0, 1, 0),
      Array(1, 1, 0),
      Array(2, 3, 7, 15),
      Array("hello", 20, 30),
    )) {
      val result = arr match {
        case Array(0) => "0"
        case Array(1, 0) => "(1, 0)"
        case Array(x, y) => "Array: " + x + "," + y //匹配两元素数组
        case Array(0, _*) => "以零开头的数组" //匹配两元素数组
        case Array(x, 1, z) => "中间为1的三元数组"
        case _ => "something else"
      }
      println(result)
    }


    println("........匹配列表")
    for (list <- List(
      List(0),
      List(1, 0),
      List(0, 0, 0),
      List(1, 1, 0),
      List(88),
      List("hello"),
    )) {
      val result = list match {
        case List(0) => 0
        case List(x, y) => "List(x y): " + x + "," + y
        case List(0, _*) => "List(0，。。。。。): "
        case List(a) => "List(a): " + a
        case _ => "something else"
      }
      println(result)
    }


    //方式二、
    var list = List(1, 2, 5, 7, 24)
    list match {
      case first :: second :: rest =>
        println(s"first: $first ,second $second, rest $rest ")
      case _ => println("something else")

    }


    println("......匹配元组............")
    //匹配元组
    for (tuple <- List(
      (0, 1),
      (0, 0),
      (0, 1, 0),
      (0, 1, 1),
      (1, 23, 56),
      ("hello", true, 0.5),
    )) {
      tuple match {
        case (a, b) => "" + a + "," + b
        case (0, _) => "(0,_)"
        case (a, 1, _) => "(a,1_)" + a
        case _ => "something else"
      }
    }


  }
}
